package com.cloudcc.mobile.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.ChatUser;
import com.cloudcc.mobile.entity.chat.DraftBox;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.view.activity.DraftBoxActivity;
import com.cloudcc.mobile.view.dynamic.SendTimeLineActivity;
import com.litesuits.android.async.Log;
import com.mypage.utils.SaveTemporaryData;

/**
 * 草稿箱适配器
 *
 * @author Administrator
 */
public class DraftBoxAdapter extends BaseAdapter {
    private String mEn = RunTimeManager.getInstance().getlanguage();
    private List<DraftBox> list;
    private Activity context;
    private OnSendListener onSendListener;

    public static Map<Integer, Boolean> isShow;
    public static Map<Integer, Boolean> isBtn;

    public DraftBoxAdapter(List<DraftBox> list, Activity context) {
        this.list = list;
        this.context = context;
        isShow = new HashMap<>();
        isBtn = new HashMap<>();
        addMap();
    }

    public void changData(List<DraftBox> list) {
        this.list = list;
        notifyDataSetChanged();
        addMap();
    }

    public void addMap() {
        for (int i = 0; i < list.size(); i++) {
            isShow.put(i, false);
            isBtn.put(i, true);
        }
    }

    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount();
    }

    public interface OnSendListener {
        public void send(int position, View v);

        public void listviewbtn(int position);

        public void LongClicked(ContextMenu menu, int position);
    }

    public void setOnSendListener(OnSendListener onSendListener) {
        this.onSendListener = onSendListener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public DraftBox getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.draftbox_list_item, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.text = (TextView) convertView.findViewById(R.id.text);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.num = (TextView) convertView.findViewById(R.id.num_caogao);
            holder.send = (Button) convertView.findViewById(R.id.send_caogao);
            holder.btnDelete = (ImageView) convertView.findViewById(R.id.btnDelete);
            holder.listitem = (LinearLayout) convertView.findViewById(R.id.listviewbtn);
            holder.layoutBtn = (LinearLayout) convertView.findViewById(R.id.layoutBtn);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        showImage(position, holder.btnDelete);
        if (getBtn(position)){
            holder.layoutBtn.setVisibility(View.VISIBLE);
        }else{
            holder.layoutBtn.setVisibility(View.GONE);
        }
        final DraftBox box = list.get(position);
        String text = "";
        if (box.getType().equals("1")) {
            if ("en".equals(mEn)) {
                holder.name.setText("Sign");
            } else {
                holder.name.setText("签到");
            }

            text = box.getKhbf().getBfText();
        } else if (box.getType().equals("2")) {

            if ("en".equals(mEn)) {
                holder.name.setText(SaveTemporaryData.mUserName);
//                holder.name.setText("Micro post");
            } else {
                holder.name.setText(SaveTemporaryData.mUserName);
//                holder.name.setText("微贴");
            }

            Tools.i("leixing", box.getLeixing());
            if (box.getLeixing().equals("addMicroPostF")) {
                text = box.getAddMicroPostF().getBody();
            } else if (box.getLeixing().equals("addMicroPostDMany")) {
                text = box.getAddMicroPostDMany().getBody();
            } else if (box.getLeixing().equals("addMicroPostL")) {
                text = box.getAddMicroPostL().getBody();
            } else if (box.getLeixing().equals("addMicroPostV")) {
                text = box.getAddMicroPostV().getBody();
            }
        } else if (box.getType().equals("3")) {
            if ("en".equals(mEn)) {
                holder.name.setText("reply");
            } else {
                holder.name.setText("回复");
            }

            text = box.getComment().getBody();
        }

        holder.time.setText(box.getTime().substring(5, box.getTime().length() - 3));
        Log.d("66666666666666666666666", box.getTime());
        holder.num.setText("" + (position + 1));
        if (!Tools.isNull(text)) {
            if (text.length() > 20) {
//                String neirong = text.substring(0, 19);
                holder.text.setText(text);
            } else {
                holder.text.setText(text);
            }
        }
        holder.send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean flag = box.isClickable();
                if (box.isClickable()) {
                    onSendListener.send(position, v);
                } else {
                }
            }
        });
//		holder.listitem.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				onSendListener.listviewbtn(position);
//
//
//
//
//                DraftBox box = list.get(position);
//                String text = "";
//                if (box.getLeixing().equals("addMicroPostF")) {
//                    text = box.getAddMicroPostF().getBody();
//                } else if (box.getLeixing().equals("addMicroPostDMany")) {
//                    text = box.getAddMicroPostDMany().getBody();
//                } else if (box.getLeixing().equals("addMicroPostL")) {
//                    text = box.getAddMicroPostL().getBody();
//                } else if (box.getLeixing().equals("addMicroPostV")) {
//                    text = box.getAddMicroPostV().getBody();
//                }
//
//                Intent intent = new Intent(context, SendTimeLineActivity.class);
//                intent.putExtra("content", text);
//                intent.putExtra("POSITION",String.valueOf(position));
//                context.startActivity(intent);
//			}
//		});

//		holder.listitem
//				.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
//					@Override
//					public void onCreateContextMenu(ContextMenu menu, View v,
//							ContextMenuInfo menuInfo) {
//						onSendListener.LongClicked(menu, position);
//					}
//				});
        return convertView;
    }

    class ViewHolder {
        TextView name;
        TextView time;
        TextView text;
        TextView num;
        Button send;
        LinearLayout listitem;
        ImageView btnDelete;
        LinearLayout layoutBtn;
    }


    public void showImage(final int position, ImageView img) {

        if (getImage(position)) {
            img.setVisibility(View.VISIBLE);
        } else {
            img.setVisibility(View.GONE);
        }
        img.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //回调传递点击的view
                mItemOnClickListener.itemOnClickListener(view, position);
            }
        });

    }

    public static void setImage(int i, boolean bool) {
        isShow.put(i, bool);
    }

    public static boolean getImage(int i) {

        return isShow.get(i);
    }


    public static void setBtn(int i, boolean b) {
        isBtn.put(i, b);
    }

    public static boolean getBtn(int i) {

        return isBtn.get(i);
    }


    private ItemOnClickListener mItemOnClickListener;

    public void setmItemOnClickListener(ItemOnClickListener listener) {

        this.mItemOnClickListener = listener;
    }

    public interface ItemOnClickListener {
        /**
         * 传递点击的view
         *
         * @param view
         * @param position
         */
        public void itemOnClickListener(View view, int position);
    }

    //------------------

}
